#include "waitKey.hpp"

struct gpiod_chip *key_chips[3] = {NULL};
struct gpiod_line *key_lines[8] = {NULL};

struct key
{
    u_int8_t state;
    u_int8_t flag;
}Key[8];

void Key_init(void)
{
    //设置gpio芯片，使用0，1，4，6
    key_chips[0] = gpiod_chip_open("/dev/gpiochip0");
    if(key_chips[0] == NULL)
        perror("chip0open");
    key_chips[1] = gpiod_chip_open("/dev/gpiochip1");
    if(key_chips[1] == NULL)
        perror("chip1open");
    key_chips[2] = gpiod_chip_open("/dev/gpiochip2");
    if(key_chips[2] == NULL)
        perror("chip2open");

    //获取gpio线路，共8个按键
    //GPIO1_C0 OK
    key_lines[0] = gpiod_chip_get_line(key_chips[1],16);
    //GPIO1_C1 RECODE
    key_lines[1] = gpiod_chip_get_line(key_chips[1],17);
    //GPIO1_B6 MENU
    key_lines[2] = gpiod_chip_get_line(key_chips[1],14);
    //GPIO1_B7 AWB
    key_lines[3] = gpiod_chip_get_line(key_chips[1],15);
    //GPIO1_A2 UP
    key_lines[4] = gpiod_chip_get_line(key_chips[1],2);
    //GPIO2_C3 RIGHT
    key_lines[5] = gpiod_chip_get_line(key_chips[2],19);
    //GPIO1_D6 LEFT
    key_lines[6] = gpiod_chip_get_line(key_chips[1],30);
    //GPIO1_D7 DOWN
    key_lines[7] = gpiod_chip_get_line(key_chips[1],31);

    struct gpiod_line_request_config config = {
        .consumer = "my-app",
        .request_type = GPIOD_LINE_REQUEST_DIRECTION_INPUT,
        .flags = GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP  // 关键：启用上拉
    };

    //设置为输入模式
    for (int i = 0; i < 8; i++)
    {
        gpiod_line_request(key_lines[i],&config,0);
    }

    for (int i = 0; i < 8; i++)
    {
        Key[i].state = 1;
        Key[i].flag = 0;
    }

}

u_int8_t Key_scan(void)
{
    u_int8_t valid_key = 0;
    //读取每个按键的值
    for (int i = 0; i < 8; i++)
    {
        Key[i].state = gpiod_line_get_value(key_lines[i]);
    }

    for (int i = 0; i < 8; i++)
    {
        switch (Key[i].flag)
        {
            case 0:
                if(Key[i].state == 0)
                    Key[i].flag = 1;
                break;
            case 1:
                if(Key[i].state == 0)
                {
                    Key[i].flag = 2;
                    valid_key = i + 1;
                }
                else
                    Key[i].flag = 0;
                break;
            case 2:
                if(Key[i].state != 0)
                    Key[i].flag = 0;
                break;
            default:
                break;
        }
    }
    return valid_key;
}

void Key_process(MENU *menu,u_int8_t key_num,char *file_path)
{
    switch(key_num)
    {
        case 1://OK
            break;
        case 2://RECODE
        if(menu->text)
            if(menu->record == false)
            {
                if(WEXITSTATUS(system("mount /dev/sda1 /mnt/usb")) != 0)//system中执行的命令的返回值
                {
                    menu->record = false;
                    menu->mount = 1;//挂载失败
                    perror("mount");
                    return;
                }
                printf("挂载成功\n");
                menu->mount = 2;
                record_init(1920,1080,file_path);
                menu->record = true;
            }
            else if(menu->record == true)
            {
                record_close();//先关闭录屏，等待10us再弹出优盘，确保保存成功
                usleep(10);
                system("umount /mnt/usb");
                menu->mount = 3;//弹出成功
                menu->record = false;
            }
            break;
        case 3://MENU
            menu->text = !menu->text;
            break;
        case 4://AWB
        if(menu->text)
            menu->AWB = 1;//启动一次校准
            break;
        case 5://UP
            if(menu->text)
            if(menu->target > 1)
                menu->target--;
            else 
                menu->target = 9;
            break;
        case 6://RIGHT
            if(menu->text)
            switch(menu->target)
            {
                case 1://亮度调节
                    if(menu->brightness < 200)
                        menu->brightness += 10;
                    else
                        menu->brightness = 0;
                    break;
                case 2://对比度调节
                    if(menu->contrast < 2.0)
                        menu->contrast += 0.1;
                    else
                        menu->contrast = 0.0;
                    break;
                case 3://红色色调调节
                    if(menu->r_hue < 100)
                        menu->r_hue += 5;
                    else
                        menu->r_hue = 0;
                    break;
                case 4://绿色色调调节
                    if(menu->g_hue < 100)
                        menu->g_hue += 5;
                    else
                        menu->g_hue = 0;
                    break;
                case 5://蓝色色调调节
                    if(menu->b_hue < 100)
                        menu->b_hue += 5;
                    else
                        menu->b_hue = 0;
                    break;
                case 6://放大倍数调节
                    menu->multiple = !menu->multiple;
                    break;
                case 7://锐度调节
                    if(menu->sharpness < 100)
                        menu->sharpness += 5;
                    else
                        menu->sharpness = 0;
                    break;
                case 8://模糊度调节
                    if(menu->blur < 100)
                        menu->blur += 10;
                    else
                        menu->blur = 0;
                    break;
                case 9://遮罩开关
                    menu->mask = !menu->mask;
                    break;
                default:
                    break;
            }
            break;
        case 7://LEFT
            if(menu->text)
            switch(menu->target)
            {
                case 1://亮度调节
                    if(menu->brightness >= 10)
                        menu->brightness -= 10;
                    else    
                        menu->brightness = 200;
                    break;
                case 2://对比度调节
                    if(menu->contrast >= 0.1)
                        menu->contrast -= 0.1;
                    else
                        menu->contrast = 2.0;
                    break;
                case 3://红色色调调节
                    if(menu->r_hue > 0)
                        menu->r_hue -= 5;
                    else
                        menu->r_hue = 100;
                    break;
                case 4://绿色色调调节
                    if(menu->g_hue > 0)
                        menu->g_hue -= 5;
                    else
                        menu->g_hue = 100;
                    break;
                case 5://蓝色色调调节
                    if(menu->b_hue > 0)
                        menu->b_hue -= 5;
                    else
                        menu->b_hue = 100;
                    break;
                case 6://放大倍数调节
                    menu->multiple = !menu->multiple;
                    break;
                case 7://锐度调节
                    if(menu->sharpness > 0)
                        menu->sharpness -= 5;
                    else
                        menu->sharpness = 100;
                    break;
                case 8://模糊度调节
                    if(menu->blur > 0)
                        menu->blur -= 10;
                    else
                        menu->blur = 100;
                    break;
                case 9://遮罩开关
                    menu->mask = !menu->mask;
                    break;
                default:
                    break;
            }
            break;
        case 8://DOWN
            if(menu->text)
            if(menu->target < 9)
                menu->target++;
            else
                menu->target = 1;
            break;
        default:
            break;
    }
}
    
void Key_close(void)
{
    for (int i = 0; i < 8; i++)
    {
        gpiod_line_release(key_lines[i]);
    }

    for (int i = 0; i < 3; i++)
    {
        gpiod_chip_close(key_chips[i]);
    }
}